import { Routes, Route, Navigate, useLocation } from 'react-router-dom'
import { useAuth } from '../components/common/useAuth'
import { ROUTES } from './routes'

// 页面组件
import LoginPage from '../pages/LoginPage/LoginPage'
import HomePage from '../pages/HomePage/HomePage'
import VideoPage from '../pages/VideoPage/VideoPage'
import SingleVideoPage from '../pages/SingleVideoPage/SingleVideoPage'
import CollectionPage from '../pages/CollectionPage/CollectionPage'
import KeywordPage from '../pages/KeywordPage/KeywordPage'
import TopicPage from '../pages/TopicPage/TopicPage'
import MyHomePage from '../pages/MyHomePage/MyHomePage'

// 布局和路由组件
import Layout from '../components/common/Layout'
import ProtectedRoute from '../components/common/ProtectedRoute'

/**
 * 应用路由配置
 */
function AppRoutes() {
  const { isLoggedIn } = useAuth()
  const location = useLocation()

  // 调试信息：显示当前路径和匹配的路由
  console.log('当前路径:', location.pathname)

  return (
    <Routes>
      {/* 登录页面 */}
      <Route
        path={ROUTES.LOGIN}
        element={
          isLoggedIn ? (
            <Navigate to={ROUTES.HOME} replace />
          ) : (
            <LoginPage />
          )
        }
      />

      {/* 受保护的路由 */}
      <Route
        path={ROUTES.HOME}
        element={
          <ProtectedRoute>
            <Layout />
          </ProtectedRoute>
        }
      >
        {/* 主页管理 */}
        <Route index element={<HomePage />} />
        
        {/* 视频列表 */}
        <Route path="videos" element={<VideoPage />} />
        
        {/* 单作品提取 */}
        <Route path="single-video" element={<SingleVideoPage />} />
        
        {/* 合集提取 */}
        <Route path="collection" element={<CollectionPage />} />
        
        {/* 关键词提取 */}
        <Route path="keyword" element={<KeywordPage />} />
        
        {/* 话题提取 */}
        <Route path="topic" element={<TopicPage />} />
        
        {/* 我的主页提取 */}
        <Route path="my-profile" element={<MyHomePage />} />
      </Route>

      {/* 404 - 根据登录状态重定向 */}
      <Route 
        path="*" 
        element={
          isLoggedIn ? (
            <Navigate to={ROUTES.HOME} replace />
          ) : (
            <Navigate to={ROUTES.LOGIN} replace />
          )
        } 
      />
    </Routes>
  )
}

// 显式导出所有页面组件，避免 TypeScript 未使用警告export { default as HomePage } from '../pages/HomePage/HomePage'export { default as LoginPage } from '../pages/LoginPage/LoginPage'export { default as VideoPage } from '../pages/VideoPage/VideoPage'export { default as SingleVideoPage } from '../pages/SingleVideoPage/SingleVideoPage'export { default as CollectionPage } from '../pages/CollectionPage/CollectionPage'export { default as KeywordPage } from '../pages/KeywordPage/KeywordPage'export { default as TopicPage } from '../pages/TopicPage/TopicPage'export { default as MyHomePage } from '../pages/MyHomePage/MyHomePage'
export default AppRoutes

